import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Copyright 2013 李松泉 
 * 功能说明：TODO 
 * 开发时间：2013-12-23下午8:16:20
 * 开发作者：李松泉
 */
public class AutoUpdateThread extends Thread {   
  
    private String filename = "czp.jar";   
    private static String xiheportalURL = "http://localhost/test/czp.jar";   
    //private static String xiheportalURL = "http://10.157.3.21:8080/powernet-graphic-czpinerfaceomsClient/update/file/czp.jar";   
    
    public static void main(String[] arg) {   
        try {   
        	AutoUpdateThread thread = new AutoUpdateThread();
        	thread.run();
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
    }   
  
    @Override  
    public void run() {   
        //JFrame uj = null;   
        String message = null;   
        String title = null;   
  
        try {   
//            uj = new JFrame();//提示用户正在更新，更新完成自动关闭   
//            uj.setLocationRelativeTo(null);//居中显示   
//            uj.setResizable(false);    
//            uj.setVisible(true);   
  
            byte[] buffer = getXiheJarFormServer();//自动下载最新的jar包   
            if (buffer == null) {   
                message = "更新失败！原因：从XXXX上下载jar包失败！";   
                title = "Error";   
            } else {   
                autoUpdate(buffer);//替换本地jar包   
                message = "更新成功！ 重新启动？";   
                title = "Message";   
            }   
        } catch (Exception e) {   
            message = "更新失败！原因：" + e.getMessage();   
            title = "Error";   
            e.printStackTrace();   
        } finally {   
            
//            int i = JOptionPane.showConfirmDialog(null, message, title,   
//                    JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null);   
//            if (i == 0) {   
//                reStartXihetool();   
//            }   
        }   
  
    }   
  
    public void reStartXihetool() {//重新运行新版jar包   
        try {   
            File dir = new File("");   
            String basepath = dir.getAbsolutePath();   
            Runtime rt = Runtime.getRuntime();   
            Process p = rt.exec("java -cp \"" + basepath + "/" + filename + "\" com.test skipUpdate");// com.test为jar主类   
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
    }   
  
    public byte[] getXiheJarFormServer() throws Exception {   
        URL target = new URL(xiheportalURL);   
        HttpURLConnection conn = (HttpURLConnection) target.openConnection();   
        conn.setDoOutput(true);   
        conn.setDoInput(true);   
        conn.setRequestMethod("GET");   
        conn.setRequestProperty("Content-Type", "*/*");   
        conn.setRequestProperty("Accept", "*/*");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setConnectTimeout(30000);   
        conn.setReadTimeout(30000);   
        conn.connect();   
        // check response code    
        int code = conn.getResponseCode();   
        boolean success = (code >= 200) && (code < 300);   
        byte[] buffer = null;   
        if (success) {   
            InputStream in = conn.getInputStream();   
            int size = conn.getContentLength();   
            System.out.println("size:" + size);   
            buffer = new byte[size];   
            int curr = 0, read = 0;   
            while (curr < size) {   
                read = in.read(buffer, curr, size - curr);   
                if (read <= 0) {   
                    break;   
                }   
                curr += read;   
            }   
            //  System.out.println(new String(buffer));   
            in.close();   
        }   
        return buffer;   
    }   
  
    public void autoUpdate(byte[] buffer) throws Exception {   
        File dir = new File("");   
        String basepath = dir.getAbsolutePath();   
        String newfilepath = basepath + "/" + filename;   
        System.out.println("newfilepath:" + newfilepath);   
        File newfile = new File(newfilepath);   
        OutputStream ops = new BufferedOutputStream(new FileOutputStream(newfile));   
        ops.write(buffer);   
        ops.close();   
    }   
}